package core;

import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;

public class TimeOfTheClasses {
	
	private int m_timeNumber;
	private static final String m_times[] = { "08:00-09:35", "09:45-11:20", "11:30-13:05",
											"13:30-15:00", "15:10-16:40", "16:50-18:20", 
											"18:30-20:00" };

	public TimeOfTheClasses(int timeNumber){
		setTime(timeNumber);
	}
	
	public void setTime(int timeNumber){
		if(timeNumber >= m_times.length)
			m_timeNumber = m_times.length - 1;
		else if(timeNumber < 0)
			m_timeNumber = 0;
		else
			m_timeNumber = timeNumber;
	}
	
	public String getTimeDescription(){
		return m_times[m_timeNumber];
	}
	
	public int getTimeNumber(){
		return m_timeNumber;
	}

	public boolean equals(Object other) {
		TimeOfTheClasses otherTime = (TimeOfTheClasses)other;
		return m_timeNumber == otherTime.m_timeNumber;
		
	}
	
	public void toXml(Node node){
		Element timeNode = node.createElement(null, "Time");
		timeNode.setAttribute(null, "number", new Integer(m_timeNumber).toString());
		node.addChild(Node.ELEMENT, timeNode);        
	}

	public static TimeOfTheClasses fromXml(Element elem) throws Throwable{
		try
	    {
			Element rootElem = elem; 
			if (!rootElem.getName().equals("Time")){
				rootElem = elem.getElement(null, "Time");
			}
			
			int num = Integer.parseInt(rootElem.getAttributeValue(null, "number"));
			return new TimeOfTheClasses(num);
	    }
		catch (Exception e) {
			throw e;
		}
	}
	
}

